package com.ettractions.kiosk.mediators
{
	import com.ettractions.kiosk.components.CoverFlowBrochureCarousel;
	import com.ettractions.kiosk.events.BrochureSelectEvent;
	import com.ettractions.kiosk.events.ViewEvent;
	import com.ettractions.kiosk.models.KioskModel;
	import com.ettractions.kiosk.vo.BrochureVO;
	
	import flash.events.Event;
	
	import mx.collections.ArrayList;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	public class CoverFlowBrochureCarouselMediator extends Mediator
	{
		public static const LIST_ITEM_COUNT:uint = 5;
		
		[Inject]
		public var model:KioskModel;
		
		[Inject]
		public var view:CoverFlowBrochureCarousel;
		
		private var _hiddenListItemIndex:int = -1;
		
		override public function onRegister():void
		{
			view.visible = false;
			
			eventDispatcher.addEventListener(ViewEvent.VIEW_INITIALIZED, handleViewInitialized);
			eventDispatcher.addEventListener(BrochureSelectEvent.BROCHURE_SELECTED, handleBrochureSelected);
			
			view.list.addEventListener(IndexChangeEvent.CHANGE, handleListIndexChange);
		}
		
		private function handleViewInitialized(event:ViewEvent):void
		{
			var listDataProvider:ArrayList = new ArrayList();
			
			for each (var brochure:BrochureVO in model.brochures)
				listDataProvider.addItem(brochure);
			
			view.list.dataProvider = listDataProvider;
			
			// eventDispatcher.removeEventListener(ViewEvent.VIEW_INITIALIZED, handleViewInitialized);
		}
		
		/*
		private function handleBrochureSelected(event:ModelEvent):void
		{
			view.visible = event.index > -1;
			
			if (view.visible)
			{
				var listDataProvider:ArrayList = new ArrayList();
				var listStartIndex:int = event.index - Math.floor(LIST_ITEM_COUNT / 2);
				
				if (listStartIndex < 0)
					listStartIndex = model.brochures.length + listStartIndex;
				
				for (var i:uint=listStartIndex; i<model.brochures.length; i++)
				{
					listDataProvider.addItem(model.brochures[i]);
					
					if (listDataProvider.length == LIST_ITEM_COUNT)
						break;
				}
				
				if (listDataProvider.length < LIST_ITEM_COUNT)
				{
					var addItemCount:uint = LIST_ITEM_COUNT - listDataProvider.length;
					
					for (i=0; i<addItemCount; i++)
						listDataProvider.addItem(model.brochures[i]);
				}
				
				view.list.dataProvider = listDataProvider;
				view.list.selectedIndex = Math.floor(LIST_ITEM_COUNT / 2);
			}
		}
		*/

		private function handleBrochureSelected(event:BrochureSelectEvent):void
		{
			if (_hiddenListItemIndex > -1)
			{
				view.list.dataGroup.getElementAt(_hiddenListItemIndex).visible = true;
				
				_hiddenListItemIndex = -1;
			}
			
			view.brochure.visible = false;
			
			view.visible = event.index > -1;
			
			if (view.visible)
			{
				view.list.selectedIndex = view.coverflow.selectedIndex = event.index;
				
				view.coverflow.addEventListener(Event.COMPLETE, handleCoverFlowTransitionComplete);
			}
		}
		
		private function handleListIndexChange(event:IndexChangeEvent):void
		{
			// model.selectedBrochureIndex = event.newIndex;
		}
		
		private function handleCoverFlowTransitionComplete(event:Event):void
		{
			_hiddenListItemIndex = view.list.selectedIndex;
			
			view.coverflow.removeEventListener(Event.COMPLETE, handleCoverFlowTransitionComplete);
			
			view.list.dataGroup.getElementAt(_hiddenListItemIndex).visible = false;
				
			view.brochure.visible = true;
		}
	}
}

/*
import mx.events.CollectionEvent;

import nl.demonsters.debugger.MonsterDebugger;

import spark.events.IndexChangeEvent;

public var brochureIndex:int;

private function handleCreationComplete():void
{
var i:uint;

dp.disableAutoUpdate();
dp2.disableAutoUpdate();

for (i = 0; i < 10; i++)
dp.addItem({
number:i + 1,
image:"jpg/image_large.jpg"
});

dp2.addItem(dp.getItemAt(dp.length - 2));			
dp2.addItem(dp.getItemAt(dp.length - 1));
dp2.addItem(dp.getItemAt(0));
dp2.addItem(dp.getItemAt(1));
dp2.addItem(dp.getItemAt(2));

dp.enableAutoUpdate();
dp2.enableAutoUpdate();

brochureIndex = 0;
selectedIndex = coverflow.selectedIndex = 2;

addEventListener(IndexChangeEvent.CHANGE, handleListIndexChange);
}

private function handleListIndexChange(event:IndexChangeEvent):void
{
removeEventListener(IndexChangeEvent.CHANGE, handleListIndexChange);

if (event.newIndex > event.oldIndex)
brochureIndex++;

else
brochureIndex--;

if (brochureIndex < 0)
brochureIndex = dp.length - 1;

else if (brochureIndex > dp.length - 1)
brochureIndex = 0;

coverflow.selectedIndex = selectedIndex;

coverflow.addEventListener(Event.COMPLETE, handleCoverFlowTransitionComplete);			
}

private function handleCoverFlowTransitionComplete(event:Event):void
{
var brochureIndeces:Array = [0, 0, brochureIndex, 0, 0];

coverflow.removeEventListener(Event.COMPLETE, handleCoverFlowTransitionComplete);				

brochureIndeces[1] = brochureIndex > 0
? brochureIndex - 1
: dp.length - 1;

brochureIndeces[0] = brochureIndeces[1] > 0
? brochureIndeces[1] - 1
: dp.length - 1;

brochureIndeces[3] = brochureIndex < dp.length - 1
? brochureIndex + 1
: 0;

brochureIndeces[4] = brochureIndeces[3] < dp.length - 1
? brochureIndeces[3] + 1
: 0;

MonsterDebugger.trace(this, brochureIndeces);

dp2.disableAutoUpdate();			

for (var i:uint=0; i<brochureIndeces.length; i++)
dp2.setItemAt(dp.getItemAt(brochureIndeces[i]), i);

dp2.enableAutoUpdate();

selectedIndex = coverflow.selectedIndex = 2;

addEventListener(IndexChangeEvent.CHANGE, handleListIndexChange);
} 
*/